#	默认编译为x86环境下app（调试用）
#	编译hisiv环境下app  make HIS=1 
#	编译arm3352环境下app  make ARM=1

#cross compile tool
ARM_PREFIX=
ifdef HIS
	ARM_PREFIX=arm-hisiv600-linux-
	MASTER_FLAG=-D__SLAVE
else 
	ifdef ARM	 
	ARM_PREFIX=arm-linux-gnueabihf-
	MASTER_FLAG=-D__MASTER
	endif
endif

#	release版本：make RELEASE=1
ifdef RELEASE
	RELEASE_FLAG=-D__RELEASE_HIENC
else
	RELEASE_FLAG=-D__DEBUG_HIENC
endif

CC=$(ARM_PREFIX)gcc
CPP=$(ARM_PREFIX)g++
AR=$(ARM_PREFIX)ar
LD=$(ARM_PREFIX)ld
STRIP=$(ARM_PREFIX)strip

HIS_TARGET = ./BWHIENCAPP_sl
HIS_DEBUG_TARGET = ./BWHIENCAPP_sl_g
ARM_TARGET = ./BWHIENCAPP_mt
ARM_DEBUG_TARGET = ./BWHIENCAPP_mt_g

ifdef HIS
	TARGET = $(HIS_TARGET)
	DEBUG_TARGET = $(HIS_DEBUG_TARGET)
else
	ifdef ARM
	TARGET = $(ARM_TARGET)
	DEBUG_TARGET = $(ARM_DEBUG_TARGET)
	else
	TARGET = ./BWHIENCAPP
	DEBUG_TARGET = ./BWHIENCAPP_g
	endif
endif

CSOURCES = 
CPPSOURCES = 
HEADS = 

SRC = ./src
CPPSOURCES += $(wildcard $(SRC)/*.cpp)
CSOURCES += $(wildcard $(SRC)/*.c)

ifdef HIS
HISAPPSRC=./hisapp
CPPSOURCES += $(wildcard $(HISAPPSRC)/*.cpp)
CSOURCES += $(wildcard $(HISAPPSRC)/*.c)

HIRTC=./rtc
CPPSOURCES += $(wildcard $(HIRTC)/*.cpp)
CSOURCES += $(wildcard $(HIRTC)/*.c)
endif

COMMSRC=../common
CPPSOURCES += $(wildcard $(COMMSRC)/*.cpp)
CSOURCES += $(wildcard $(COMMSRC)/*.c)

LIBUSRC=../lib/libunisky/src
CPPSOURCES += $(wildcard $(LIBUSRC)/*.cpp)
CSOURCES += $(wildcard $(LIBUSRC)/*.c)

COBJS = $(patsubst %.c, %.o, $(CSOURCES))
CPPOBJS = $(patsubst %.cpp, %.o, $(CPPSOURCES))

HEADPATH += ./inc
HEADPATH += ./inc/uc
HEADPATH += ./inc/hisi
HEADPATH += ./inc/ff35
HEADPATH += ../lib/libunisky/inc
HEADPATH += ../lib/

HEADPATH +=	./inc/freetype
HEADPATH +=	./inc/freetype/config
HEADPATH +=	./inc/SDL
HEADPATH +=	./inc/CxImage
HEADS += $(wildcard inc/*.h)

INC = $(patsubst %,-I%,$(HEADPATH)) 


OPTIM=
DEBUG=-g 
STRIP=-s

ifdef HIS
HISDK_LIB = $(LIB_DIR)/hisi
LIB= -L./lib -L./lib/hisi -L./lib/ff35 -L./lib/freetype -L./lib/SDL -L./lib/SDL_ttf

CFLAGS = -std=c99 -D_GNU_SOURCE $(DEBUG) $(OPTIM) -Wall -Dhi3531dv100 -DHICHIP=0x3531d100 -DHI_DEBUG -DHI_XXXX -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=neon -mno-unaligned-access -fno-aggressive-loop-optimizations -ffunction-sections $(INC) $(RELEASE_FLAG) $(MASTER_FLAG)
CPPFLAGS = -std=gnu++0x $(DEBUG) $(OPTIM) -Wall -Dhi3531dv100 -DHICHIP=0x3531d100 -DHI_DEBUG -DHI_XXXX -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=neon -mno-unaligned-access -fno-aggressive-loop-optimizations -ffunction-sections $(INC)  $(RELEASE_FLAG) $(MASTER_FLAG)
	 
LDFLAGS1 = $(LIB) -Wl,-rpath -lrt -lm \
	-lmpi -laacenc -laacdec -lSDL -lSDL_ttf -lfreetype -lupvqe -lVoiceEngine -ldnvqe -ljpeg  -lCxImage \
	-lavformat -lavcodec -lavutil -lpostproc -lswresample -lswscale -lavfilter -lavresample -lutils -lpthread 	   
else
LIB=

CFLAGS = -std=c99 -D_GNU_SOURCE $(DEBUG) $(OPTIM) -Wall $(INC) $(RELEASE_FLAG) $(MASTER_FLAG)
CPPFLAGS = -std=gnu++0x $(DEBUG) $(OPTIM) -Wall $(INC)  $(RELEASE_FLAG) $(MASTER_FLAG)

LDFLAGS1 = $(LIB) -lpthread -lrt -lm
endif

LDFLAGS = $(LDFLAGS1)

all: $(TARGET) $(DEBUG_TARGET)

$(TARGET): $(CPPOBJS) $(COBJS)
	$(CPP) $(STRIP) $(CPPOBJS) $(COBJS) \
	$(LDFLAGS) \
	-o $(TARGET) 
	
$(DEBUG_TARGET): $(CPPOBJS) $(COBJS)
	$(CPP) $(CPPOBJS) $(COBJS) \
	$(LDFLAGS) \
	-o $(DEBUG_TARGET)
#scp BWHIENCAPP_sl_g root@192.168.100.80:/tmp
	cp BWHIENCAPP_sl_g /home/aston/rootfs/

%.o:%.c $(HEADS)
	$(CC) $(CFLAGS) \
	-c $<  -o $@
	
%.o:%.cpp $(HEADS)
	$(CPP) $(CPPFLAGS) \
	-c $<  -o $@
	
clean: 
	rm -f  $(CPPOBJS) $(COBJS) ./hisapp/*.o \
 	$(TARGET) $(DEBUG_TARGET) \
	$(HIS_TARGET) $(HIS_DEBUG_TARGET) \
	$(ARM_TARGET) $(ARM_DEBUG_TARGET)
